var nickName = sessionStorage.getItem('SCS_NICK_NAME');
if(nickName == null || nickName == '' || typeof nickName == 'undefined'){
	nickName = sessionStorage.getItem('SCS_USER_NAME');
}
if (nickName != null && nickName != '') {
	setUserLogin(nickName);
}else {
	$('#myUserName').remove();
	$('.user-register,.user-login').show();
}

$(function() {
	$('#IamLogout').click(function() {
		userLogout();
	});
});
//设置登录用户
function setUserLogin(commonUserName) {
	$('.user-register,.user-login').hide();
	$('#IamLogout').show();
	$('#Myself').append('<a id="myUserName" href="/userPages/myServiceOrder.html">' + commonUserName + '</a>');
}
//退出
function userLogout() {
	scsUserName = sessionStorage.getItem('SCS_USER_NAME');
	if (scsUserName == null || scsUserName == '' || typeof scsUserName == 'undefined') {
		showLoginRegister();
		return;
	}
	$.ajax({
		type: "POST",
		url: '/index/user/toLogout',
		dataType: 'json',
		cache: false,
		success: function(s) {
			delUserFromSession();
			showLoginRegister();
			//退出成功后，跳转到登录页面
			window.location.href = '/userPages/userLogin.html';
		},
		error: function() {
			showLoginRegister();
		}
	});
}

/**
 * 显示顶部菜单栏的登录注册按钮，和隐藏右上角的退出按钮
 */
function showLoginRegister() {
	$('#myUserName').remove();
	$('.user-register,.user-login').show();
	$('#IamLogout').hide();
}
/**
 * 从sessionStorage中删除用户信息
 */
function delUserFromSession() {
	sessionStorage.removeItem(SCS_USER_NAME);
	sessionStorage.removeItem(SCS_NICK_NAME);
	sessionStorage.removeItem(SCS_USER_ID);
	sessionStorage.removeItem(SCS_USER_PHONE);
}